package com.zjc.yohoshop.mvp.view.adapter;

import android.content.pm.LabeledIntent;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.zjc.yohoshop.R;
import com.zjc.yohoshop.bean.GoodsValuesEntity;
import com.zjc.yohoshop.network.API;

import java.util.ArrayList;
import java.util.List;

/**
 * 供应页面顶部轮播
 */
public class GoodsValuesHeadTopViewPagerAdapter extends PagerAdapter {

    private List<GoodsValuesEntity.RecommendGoodsBean> list = new ArrayList<>();

    public GoodsValuesHeadTopViewPagerAdapter(List<GoodsValuesEntity.RecommendGoodsBean> list) {
        this.list = list;
    }

    public List<GoodsValuesEntity.RecommendGoodsBean> getList() {
        return list;
    }

    @Override
    public int getCount() {
        if (list == null || list.size() == 0){
            return 0;
        }
        return list.size();
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
        return view == o;
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        if (position == list.size() - 1){
            View view = View.inflate(container.getContext(), R.layout.item_goods_values_top_vp, null);
            container.addView(view);
            return view;
        }else{
            ImageView img = new ImageView(container.getContext());
            Glide.with(container.getContext()).load(API.BaseUrl + list.get(position % list.size()).goods_img_path).into(img);
            container.addView(img);
            return img;
        }
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        View view = (View) object;
        container.removeView(view);
    }
}
